<template>
    <div class="header">
        <div class="left">
            <div class="collapse-bth" @click="collapseChage">
                <i v-if="!collapse" class = "el-icon-s-fold"></i>
                <i v-else class = "el-icon-s-unfold"></i>
            </div>
            <div class="logo">后台管理系统</div>
        </div>
        <div class="right">
            <!-- 全屏显示 -->
            <div class="btn-fullscreen" @click="handleFullScreen">
                <!-- el-tooltip 用来hover时，出现提示 -->
                <!-- content属性控制提示信息 -->
                <el-tooltip effect="dark" :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">
                    <i class="el-icon-rank"></i>
                </el-tooltip>
            </div>
            <!-- 消息中心 -->
            <div class="btn-bell">
                <el-tooltip effect="dark" :content="message?`有${message}条未读消息`:`消息中心`" placement="bottom">
                    <router-link to="/tabs">
                        <i class="el-icon-bell"></i>
                    </router-link>
                </el-tooltip>
                <span class="btn-bell-badge" v-if="message"></span>
            </div>
            <!-- 用户头像 -->
            <div class="user-avator">
                <img src="~assets/img/img.jpg" alt="">
            </div>
            <!-- 用户下拉菜单 -->
            <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                <!-- 下拉菜单头 -->
                <span class="el-dropdown-link">
                    {{getUserName}}
                    <i class="el-icon-caret-bottom"></i>
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                    <a href="#"  target="_blank">
                        <el-dropdown-item>项目仓库</el-dropdown-item>
                    </a>
                    <!-- common为指令，外面使用@command同一监听 -->
                    <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div>
</template>

<script>
    import bus from 'utils/bus.js'
    export default {
        name: 'Header',
        data () {
            return {
                collapse: false,
                clientWidth:0,
                fullscreen:false,
                message:2
            }
        },
        mounted () {
            this.clientWidth = document.body.clientWidth;
            window.onresize = () => {
                this.clientWidth = document.body.clientWidth;
            };
  
        },
        computed: {
          getUserName() {
              return localStorage.getItem('ms_username');
          }  
        },
        methods: {
            collapseChage() {
                this.collapse = !this.collapse;
                bus.$emit('collapse', this.collapse);
            },
            
            // 控制全屏
            handleFullScreen() {
                let html = document.documentElement;
                if (this.fullscreen) {
                    // 如果是全屏，就退出全屏
                    if (document.exitFullscreen) {
                    document.exitFullscreen();
                    } else if (document.webkitCancelFullScreen) {
                        document.webkitCancelFullScreen();
                    } else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    } else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    }
                } else {
                    if (html.requestFullscreen) {
                        html.requestFullscreen();
                    } else if (html.webkitRequestFullScreen) {
                        html.webkitRequestFullScreen();
                    } else if (html.mozRequestFullScreen) {
                        html.mozRequestFullScreen();
                    } else if (html.msRequestFullscreen) {
                        // IE11
                        html.msRequestFullscreen();
                    }
                }
                this.fullscreen = !this.fullscreen;
            },
            // 用户名下拉菜单选择事件
            handleCommand(command) {
                if (command == "loginout") {
                    const time = localStorage.getItem('time');
                    localStorage.clear();
                    localStorage.setItem('preTime', time);
                    this.$router.push('/login')
                }
            }
        },
        watch: {
            // clientWidth(newVal) {
            //     if (newVal < 1200) {
            //         this.collapse = true;
            //     }
            //     else {
            //         this.collapse = false;
            //     }  
            // }
        }
    }
</script>

<style scoped>
    .header {
        position: relative;
        box-sizing: border-box;
        width: 100%;
        height: 70px;
        font-size: 22px;
        color: #fff;
        display: flex;
        justify-content: space-between;
    }

    .left {
        width: 270px;
        height: 100%;
        margin-left: 21px;
        display: flex;
        justify-content: space-between;
        justify-items: center;
    }

    .right {
        width: 200px;
        height: 100%;
        margin-right: 50px;
        display: flex;
        align-items: center;
    }

    .left .collapse-bth {
        cursor: pointer;
        line-height: 70px;
    }

    .left .logo {
        width: 220px;
        line-height: 70px;
    }

    .right .btn-fullscreen {
        transform: rotate(45deg);
        margin-right: 5px;
        font-size: 24px;
    }

    .right .btn-fullscreen, .right .btn-bell {
        position: relative;
        width: 30px;
        height: 30px;
        border-radius: 15px;
        cursor: pointer;
    }

    .right .btn-bell .btn-bell-badge {
        position: absolute;
        right: 0;
        top: -2px;
        width: 8px;
        height: 8px;
        border-radius: 4px;
        background-color: #f56c6c;
        color: #fff;
    }

    .btn-bell .el-icon-bell {
        color: #fff;
    }

    .user-avator {
        margin-left: 10px;
    }

    .user-avator img {
        display: block;
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }

    .user-name {
        margin-left: 10px;
    }

    .el-dropdown-link {
        cursor: pointer;
        color: #fff;
    }
</style>